// std functions
#include <iostream>
#include <stdexcept>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <math.h>

// boost functions
#include <boost/program_options.hpp>

// my functions
#include <print_error.h>
#include <program_options_helper.h>
#include <stlhelper.h>
#define DEBUG_LEVEL 6
#include <debug_func.h>
#include <blast_results.h>
#include "get_arg.h"
using std::string;
using std::cerr;
using std::cout;
using std::max;




// link with -lboost_filesystem-gcc -lboost_program_options-gcc
// program_options_helper.cpp
//-lboost_filesystem-gcc-d
//-L/usr/local/lib

using namespace program_options_helper;
namespace po = boost::program_options_lg;


//_______________________________________________________________________________________

//		process_command_line_options

//				from command line
//				Created [9/13/2001]

//_______________________________________________________________________________________
bool process_command_line_options(int argc, char* argv[], t_program_args& args)
{

	//
	// checked mandatory arguments
	//
	//po::options_description mandatory_args("Mandatory Arguments");
	//mandatory_args.add_options()







	args.optional_args.add_options()
	("regex,r", 		po::value<string>(&args.regex)->set_name("REGEX")
								->default_value("(\\S+)"),
							"Regular expression for extracting the sequence identifier. "
							"Defaults to first non-whitespace accession characters.")
	("detail_errors,d", po::bool_switch(&args.detail_errors),
							"Report errors in detail\n")
    ("companion_file,c", 	po::value<string>(&args.file_companion)
                                ->set_name("FASTA"),
                             "Sequences with matching identifiers in this companion "
							 "FASTA file will be converted to using the same hashes.")
	("output_companion_file,C", po::value<string>(&args.file_companion_output)
								->set_name("FASTA"),
							 "Hashed companion file.")
	("ignore_unhashed,i", po::bool_switch(&args.ignore_unhashed),
							"Ignore sequences in the companion file which do not correspond "
							"to the hashed identifiers. The default behaviour is to exit "
							"with an error.")
    ("identifier_to_hash_map,m", 	po::value<string>(&args.file_map)
                                ->set_name("FILE")
                                ->default_value("STDOUT"),
                             "Original identifiers and their equivalent hashes in 2 "
                             "tab-delimited columns.");


	args.add_std_options();
	po::options_description all_args("");                              
	all_args.add(args.optional_args);
		//.add(mandatory_args)

	// parse command line
	po::variables_map vm;
	po::store(po::command_line_parser(argc, argv).options(all_args).run(), vm);
	po::notify(vm);

	if (vm.count("version"))
	{
		print_version(cerr, *argv, "1.1\n"
			"\tHistory: \n\n"
			"\tv 1.0\1"
			"18-02-2008\1"
			"Initial version adapted from fasta_identifiers\n"
			);
		return false;
	}

	if ( !args.file_companion.length() !=
		 !args.file_companion_output.length())
	{
		throw std::runtime_error("Both --companion_file and --output_companion_file must be "
								 "specified together or not at all.\n");
	}
	if (vm.count("help"))
	{
		string exe_description =
								"Annotates a fasta file with sequence hashes.\n"
								"Optionally takes a second fasta and annotates corresponding "
								"identifiers with the hashes calculated in the main fasta file.\n"
								"This is useful for annotating peptide files with cDNA hashes or "
								"vice versa.";
		                             
		print_usage(cerr, string(*argv), "[OPTIONS] < SEQUENCES.FASTA > HASHED.FASTA",
							all_args, exe_description, 80);
		return false;
	}
	//check_required_options(vm, mandatory_args);

	args.open_err_log();
	return true;
}


